package com.miyake.client.datamanage;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.miyake.client.DataService;
import com.miyake.client.DataServiceAsync;
import com.miyake.client.community.CommunityListDialog;
import com.miyake.client.datamanage.DataList.DataListHandler;
import com.miyake.client.tools.OkCancelDialog;
import com.miyake.client.tools.ShowDialog;
import com.miyake.client.tools.TextDialog;
import com.miyake.client.tools.UploadDialog;

public class DataManageMain extends Composite {

	private DataList dataList = null;
	private DataViewer dataViewer;
	private final HorizontalPanel horizontalPanel_1 = new HorizontalPanel();
	private DataServiceAsync service = DataService.Util.getInstance();
	private String defaultNode;
	
	public DataManageMain() {
		initialize();
	}
	
	public DataManageMain(String defaultNode) {
		this.defaultNode = defaultNode;
		initialize();
		
	}

	private boolean isMyData () {
		if (defaultNode == null) {
			return true;
		}
		return false;
	}
	
	private void initialize() {
		if (this.isMyData()) {
			dataList = new DataList();
		}
		else {
			dataList = new DataList(defaultNode);
		}
		VerticalPanel verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		verticalPanel.setWidth("700px");
		ToolBarHandler toolBarHandler = new ToolBarHandler() {
			@Override
			public void onNewRoot() {
				new ShowDialog() {
					@Override
					protected void onOK(String text) {
						createNewRoot(text);
					}

					@Override
					protected DialogBox getDialogBox() {
						return new TextDialog();
					}
				};
				
			}

			@Override
			public void onNewFolder() {
				new ShowDialog() {
					@Override
					protected
					void onOK(String text) {
						createNewFolder(dataList.getSelectedNodeKey(), text);
					}
					@Override
					protected DialogBox getDialogBox() {
						return new TextDialog();
					}
				};
				
			}

			@Override
			public void onNewData() {
				new ShowDialog() {
					@Override
					protected void onOK(String text) {
						dataList.updateActiveNode();
					}

					@Override
					protected DialogBox getDialogBox() {
						return new UploadDialog(dataList.getSelectedNodeKey());
					}
				};
			}

			@Override
			public void onDelete() {
				new ShowDialog() {
					@Override
					protected
					void onOK(String text) {
						deleteNode(dataList.getSelectedNodeKey());
					}

					@Override
					protected DialogBox getDialogBox() {
						return new OkCancelDialog();
					}
				};
				
			}

			@Override
			public void onNewText() {
				createText(new Text("New Text"));
			}

			@Override
			public void onUp() {
				moveUp();
			}

			@Override
			public void onDown() {
				moveDown();
			}

			@Override
			public void onNewTable() {
				new ShowDialog() {
					@Override
					protected
					void onOK(String text) {
						createTable(text);
					}
					
					@Override
					protected DialogBox getDialogBox() {
						return new TextDialog(dataList.getSelectedNode().getName() + " - Table");
					}
				};
				
			}

			@Override
			public void onNewImage() {
				new ShowDialog() {
					@Override
					protected void onOK(String text) {
						dataList.updateActiveNode();
					}

					@Override
					protected DialogBox getDialogBox() {
						return new UploadDialog("dataKey," + dataList.getSelectedNodeKey());
					}
				};
				
			}

			@Override
			public void onNewPaper() {
				new ShowDialog() {
					@Override
					protected
					void onOK(String text) {
						createPaper(text);
					}

					@Override
					protected DialogBox getDialogBox() {
						return new TextDialog();
					}
				};
			}

			@Override
			public void onReload() {
				dataList.initRootNodes();
			}

			@Override
			public void onNewPlot(final Integer columns) {
				final PlotDialog dlg = new PlotDialog() {
					@Override
					protected String getDefaultName() {
						return dataList.getSelectedNode().getName() + " - Chart";
					}

					@Override
					protected Integer getColumnCount() {
						return columns; // This number should be got from Data class.
					}
				};
				new ShowDialog() {
					@Override
					protected
					void onOK(String text) {
						createPlot(dlg.getName(), dlg.getXcol(), dlg.getYcol());
					}

					@Override
					protected DialogBox getDialogBox() {
						return dlg;
					}
				};
				
			}

			@Override
			public void onPost() {
				new ShowDialog() {
					@Override
					protected void onOK(String text) {
					}

					@Override
					protected DialogBox getDialogBox() {
						return new CommunityListDialog();
					}
				};
			}

			@Override
			public void onRename() {
				new ShowDialog() {

					@Override
					protected void onOK(String text) {
						rename(text);
					}

					@Override
					protected DialogBox getDialogBox() {
						return new TextDialog(dataList.getSelectedNode().getName());
					}
					
				};
			}
		};
		if (isMyData()) {
			verticalPanel.add(createToolBar(toolBarHandler));
		}
		
		dataViewer = new DataViewer();
		if (isMyData()) {
			dataViewer.addCreateHandler(toolBarHandler);
			dataViewer.addNodeUpdateHandler(new NodeUpdateHandler() {
				@Override
				public void onNodeUpdate(String key) {
					dataList.updateNode(key);
				}

				@Override
				public void onNodeChange(String nodeKey) {
					dataList.setSelectedNode(nodeKey);
				}

				@Override
				public void onNodeAdd(String nodeKey) {
					dataList.updateActiveNodePath();
				}

				@Override
				public void onSubNodeChange(String nodeKey) {
					//dataList.updateActiveNodePath();
					String key = dataList.getSelectedNodeKey();
					//dataList.initRootNodes();
					//dataList.setSelectedNode(key);
					dataList.updateActiveNodePath();
				}
			});
			dataViewer.setCreateEnabled(true);
			dataViewer.setUpdateEnabled(true);
		}
		else {
			dataViewer.addGuestOperationHandler(new GuestHandler() {
				@Override
				public void onCopy() {
					// TODO Auto-generated method stub	
				}
			});
			dataViewer.setGuestOperationEnabled(true);
		}
		verticalPanel.add(horizontalPanel_1);
		horizontalPanel_1.setWidth("100%");
		
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		horizontalPanel.setBorderWidth(1);
		verticalPanel.add(horizontalPanel);
		horizontalPanel.setWidth("100%");
		
		horizontalPanel.add(dataList);
		horizontalPanel.add(dataViewer);

		dataList.addDataListHandler(new DataListHandler() {
			@Override
			public void onSelectChange(NodeProperty node) {
				System.out.println(node.getName());
				dataViewer.setSelectedNode(node);
			}
		});
		

	}

	private ToolBar createToolBar(ToolBarHandler toolBarHandler) {
		ToolBar toolBar = new ToolBar(toolBarHandler){
			@Override
			protected String getActiveKey() {
				return dataList.getSelectedNodeKey();
			}
		};
		return toolBar;
	}

	protected void createPaper(String name) {
		service.createPaper(name, getParentKey(), new AsyncCallback<String>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(String result) {
				//dataList.updateActiveNode();
				dataList.updateActiveNodePath();
			}
		});
	}

	protected void createTable(String name) {
		service.createTable(name, getParentKey(), new AsyncCallback<String>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(String result) {
				//dataList.updateActiveNode();
				dataList.updateActiveNodePath();
			}
		});
	}

	protected void moveDown() {
		service.moveNodeDown(dataList.getSelectedNodeKey(), new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(Void result) {
				dataList.updateParentNode();
			}		
		});	
	}

	protected void moveUp() {
		service.moveNodeUp(dataList.getSelectedNodeKey(), new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(Void result) {
				dataList.updateParentNode();
			}		
		});
	}

	protected void createText(Text text) {		
		service.createText("text", getParentKey(), text, dataList.getOrder(), new AsyncCallback<String>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(String result) {
				dataList.updateParentNode();
			}
		});
	}

	protected void createPlot(String name, Integer xcol, Integer ycol) {
		service.createPlot(name, getParentKey(), xcol, ycol, new AsyncCallback<String>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(String result) {
				dataList.updateActiveNodePath();
			}		
		});
	}

	protected void deleteNode(String key) {
		service.deleteNode(key, new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {

			}

			@Override
			public void onSuccess(Void result) {
				dataList.updateParentNode();
			}	
		});
	}

	protected void createNewFolder(String selectedNodeKey, String name) {
		service.createNode(name, getParentKey(), new AsyncCallback<String>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(String result) {
				dataList.updateActiveNode();
			}
		});
	}

	protected void createNewRoot(String name) {
		service.createRootNode(name, new AsyncCallback<String>() {
			@Override
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}

			@Override
			public void onSuccess(String result) {
				dataList.initRootNodes();
			}
		});
	}
	
	private String getParentKey() {
		NodeProperty prop = dataList.getSelectedNode();
		String key;
		if (prop.getType().equals(NodeProperty.DATA) || 
				prop.getType().equals(NodeProperty.FOLDER)	|| 
				prop.getType().equals(NodeProperty.ROOTNODE) || 
					prop.getType().equals(NodeProperty.PAPER)) {
			key = prop.getKey();
		}
		else {
			key = prop.getParentKey();
		}
		return key;
	}
	
	private void rename(String text) {
		service.renameNode(dataList.getSelectedNodeKey(), text, new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {

			}

			@Override
			public void onSuccess(Void result) {
				dataList.updateActiveNode();
			}
			
		});
	}
}
